package com.xgd.phylw.service.impl;

import com.xgd.phylw.bean.Movie;
import com.xgd.phylw.bean.MovieExample;
import com.xgd.phylw.bean.Movietemp;
import com.xgd.phylw.dto.Movie_tempDto;
import com.xgd.phylw.mapper.MovieMapper;
import com.xgd.phylw.mapper.MovietempMapper;
import com.xgd.phylw.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private MovietempMapper movietempMapper;

    public List<Movie_tempDto> SelectAllMovie(Movie movie) {
        List<Movie_tempDto> list = new ArrayList<Movie_tempDto>();
        List<Movie> movies = movieMapper.selectAllMovieAndTemp(movie);
        for (Movie m:movies) {
            Movie_tempDto mt = new Movie_tempDto();
            mt.setMovieName(m.getMovieName());
            mt.setMovieImg(m.getMovieImg());
            mt.setMovieTime(m.getMovieTime());
            mt.setMovieProtagonist(m.getMovieProtagonist());
            mt.setMovieDirector(m.getMovieDirector());
            mt.setMovieDescribe(m.getMovieDescribe());
            mt.setMovieNum(m.getMovieNum());
            mt.setMovieRelease(m.getMovieRelease());
            String str = "";
            for (Movietemp t:m.getMovietemps()) {
                str=t.getTempName()+" ";
            }
            mt.setTempName(str);
            list.add(mt);
        }
        return list;
    }

    public Movie MovieByName(Movie movie) {
        List<Movie> movies = movieMapper.selectAllMovieAndTemp(movie);
        if(movies != null){
            for(Movie m:movies){
                return m;
            }
        }
        return null;
    }

    public Movie selectByPrimaryKey(Integer movieId) {
        return movieMapper.selectByPrimaryKey(movieId);
    }

    public List<Movie> MovieAll(String movie) {
        return movieMapper.LikeMovie("%"+movie+"%");
    }

    public List<Movie> MovieTempAll(String movietemp) {
        return movieMapper.LikeMovieTemp("%"+movietemp+"%");
    }

    public int movieTempAdd(Movie movie) {
        return movieMapper.insertSelective(movie);
    }

    public int movieDelete(String[] movie) {
        return movieMapper.deleteByName(movie);
    }

    public int movieTepDelete(Integer movieid) {
        return movietempMapper.deleteByName(movieid);
    }

    public int movieTempDel(Movie movie) {
        MovieExample example = new MovieExample();
        example.createCriteria().andMovieNameEqualTo(movie.getMovieName());
        return movieMapper.updateByExampleSelective(movie,example);
    }

    public int movieTempInsert(Movietemp movietemp) {
        return movietempMapper.insert(movietemp);
    }

    public int movieTempById(Movie movie) {

        return movieMapper.updateByPrimaryKeySelective(movie);
    }

    public List<Movie> selectMovie() {
        List<Movie> movies = movieMapper.selectAllMovie();
        List<Movie> newMovies = new ArrayList<Movie>();
        for(Movie movie:movies){
            if (movie.getMovieTemp() == 0) {
                newMovies.add(movie);
            }
        }
        return newMovies;
    }
}
